package situ.autoweter.tools;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public class GetFileItemlsForMVC {


	//基本数据类型
	private static String[] baseType;
	//引用类型Class
	private static Class[] type;
    private static List<String> baseTypeList;
	private static List<Class>  typeList;
	//基本类型和引用类型键值对map
	private static  Map<String,Class> typeMap=new HashMap<>(8);


    static{
		baseType=new String[]{"int","double","long","short","byte","boolean","char","float"};
		type=new Class[]{Integer.class,Double.class,Long.class,Short.class,Byte.class,Boolean.class,Character.class,Float.class};
		baseTypeList=Arrays.asList(baseType);
		typeList=Arrays.asList(type);

		for (int i = 0; i <baseType.length ; i++) {
			typeMap.put(baseType[i],type[i]);
		}


	}


	//获取文本域值
	public static Map<String,String> getFileItems(MultipartHttpServletRequest request) throws Exception {
		
		Enumeration<String> parameterNames = request.getParameterNames();
	
	    
         Map<String,String> map=new ConcurrentHashMap<>();
   
   
   
			   while(parameterNames.hasMoreElements()) {

				 String key=  parameterNames.nextElement();

				 map.put(key, request.getParameter(key));

			   }
	    return map;
		}
	
	//获取文本域值并封装成对象
		public static Object getFileItemsObject(Object object,MultipartHttpServletRequest request) throws Exception {
					    
		    Map<String,String> map=new ConcurrentHashMap<>();
		    
		    Enumeration<String> parameterNames = request.getParameterNames();
		    
		    while(parameterNames.hasMoreElements()) {
		    	
		    	String key=parameterNames.nextElement();
				System.out.println("key :"+key);
		    	map.put(key, request.getParameter(key));
				System.out.println("key value :"+request.getParameter(key));
		    }
		    
			
			Field[] f=object.getClass().getDeclaredFields();

			for (Field field : f) {
				field.setAccessible(true);
				//获取属性的名称
				String fieldName=field.getName();
				System.out.println(fieldName);
				//获取属性应该赋予的字面值
				String value=map.get(fieldName);
				System.out.println("value:  "+value);
			    //给属性赋值
				if(value!=null&&value.trim()!="") {
                 //String final 不可被继承
				if(field.getType().getName().equals("java.lang.String")) {

					field.set(object, value);
					continue;
				};
                 //java.lang.Number抽象类是java.math.BigDecimal和基本数字包装类型的父类
				if(Number.class.isAssignableFrom(field.getType())){
					//获取有参构造
					Constructor constructor =field.getType().getConstructor(String.class);

					field.set(object, constructor.newInstance(value));
					continue;
				}

				//基本数据类型

					if(typeMap.containsKey(field.getType().getSimpleName().toString())){

						Class clazz=typeMap.get(field.getType().getSimpleName().toString());
						Constructor constructor=clazz.getConstructor(String.class);
						field.set(object,  constructor.newInstance(value));
						continue;
					}


	           //java.util.Date是java.sql.Date和java.sql.Timestamp父类
	            if(Date.class.isAssignableFrom(field.getType())) {
					//获取有参构造
					Constructor constructor =field.getType().getConstructor(long.class);

					field.set(object,  constructor.newInstance(Long.parseLong(value)));
					continue;
				}; 
				}
				
			
			}
			
			return object;
				
			}
			
		
		//获取文件
			public static Map<String,byte[]> getFileItemsFile(MultipartFile[] file) throws Exception {
			
			    Map<String,byte[]> map=new ConcurrentHashMap<>();
				for (MultipartFile fileItem : file) {
					if(fileItem.getContentType()!=null) {
						
						map.put(fileItem.getOriginalFilename(), fileItem.getBytes());
						
					}
				}
				return map;
			}
}
